package com.optionexplorer.data;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.logging.Logger;

import com.optionexplorer.util.DateUtils;
import com.optionexplorer.util.NetUtils;
import com.optionexplorer.util.StringUtils;

public class YahooOptionsOneMonth {

	public static final Logger Log = 
		Logger.getLogger(YahooOptionsOneMonth.class.getName());
	public static String RIGHT_ANGLE = ">";
	public static String LEFT_ANGLE  = "<";	
	
	private String _stockQuote;
	private String _url;
	private String _expDate;
	
	private ArrayList<StockOption> callOptionList = null;
	public ArrayList<StockOption> getCallOptionList() {
		return callOptionList;
	}


	public ArrayList<StockOption> getPutOptionList() {
		return putOptionList;
	}


	private ArrayList<StockOption> putOptionList = null;
 	
	public YahooOptionsOneMonth(String quote, 
								String url,
								String ex) {
		this._stockQuote = quote;
		this._url = url;
		this._expDate = ex;
	}
	
	
	/**
	 * Fetches the calls and puts as needed from the Yahoo pages.
	 * @param calls
	 * @param puts
	 * @return
	 */
	public void populateOptions() {		
		
		int currPosition = 0;
		int endPosition = 0;
		int pos1 = 0;
		int pos2 = 0;
		String str = null;
		StockOption opt = null;
		float stockPrice = 0;
		boolean header = true;
		String rowStr = null;
		ArrayList<String> colList = null;
		float bidPrice, askPrice, lastPrice, premium = 0;
	
		
		StringBuilder sb = NetUtils.getPageFromWeb(this._url);
		Calendar cal = DateUtils.getCalFromStr(this._expDate);
		DateUtils.setCalToThirdFriday(cal);
		
		//Look for ET: followed by <big><b> to get stock price
		currPosition = sb.indexOf("ET");
		currPosition = sb.indexOf("<big><b>", currPosition);
		if (currPosition > 0) {
			pos1 = currPosition += 8;
			pos2 = sb.indexOf("</b></big>", pos1);
			str = sb.substring(pos1, pos2);
			str = StringUtils.stripHTML(str);
			stockPrice = Float.parseFloat(str);
		}
		Log.info("Identified stock price : " + stockPrice + " for ticket : " +
				this._stockQuote);
		
		callOptionList = new ArrayList<StockOption>();		
		Log.info("Scanning for calls");
		/**
		 * Look for CALL OPTIONS and move next two <table tags to get to first row.
		 */
		currPosition = sb.indexOf("Call Options");
		endPosition = sb.indexOf("Put Options");
		if (currPosition < 0) {
			System.out.println("Could not find CALL OPTIONS");
			return;
		}
		
		currPosition = sb.indexOf("</table", currPosition);
		currPosition = sb.indexOf("</table", currPosition);
		
		while (currPosition <= endPosition) {
			pos1 = sb.indexOf("<tr>", currPosition);
			pos2 = sb.indexOf("</tr>", pos1);
			rowStr = sb.substring(pos1, pos2);
			colList = StringUtils.rowToColumns(rowStr);
						
			if (colList == null || colList.size() < 3) {
				Log.severe("No data in colList");
			} else {
				if (header == false) {

					lastPrice = StringUtils.strToFloat(colList.get(2));
					bidPrice = StringUtils.strToFloat(colList.get(4));
					askPrice = StringUtils.strToFloat(colList.get(5));
					
					premium = lastPrice;
					if (lastPrice > bidPrice && bidPrice > 0 && askPrice > 0) {
						premium = (bidPrice + askPrice)/2;
					}
										
					opt = new CallOption(stockPrice,
							colList.get(1),
							cal.getTime(),
				            StringUtils.strToFloat(colList.get(0)),
				            premium,  // using bid price. change to 2 to get last price.
				       	    StringUtils.strToLong(colList.get(6)), 
							StringUtils.strToLong(colList.get(7)));
					Log.info("Got one option : " + opt);
					callOptionList.add(opt);							
				} else {
					header = false;
				}
			}		
			currPosition = pos2 + 5;		
		}						

		/* Fetch the PUT options */
		currPosition = sb.indexOf("Put Options");
		endPosition = sb.indexOf("Expand to Straddle View");
		header = true;
		putOptionList = new ArrayList<StockOption>();
		while (currPosition <= endPosition) {
			pos1 = sb.indexOf("<tr>", currPosition);
			pos2 = sb.indexOf("</tr>", pos1);
			rowStr = sb.substring(pos1, pos2);
			colList = StringUtils.rowToColumns(rowStr);
			
			/* Position 0 : Strike Price
			 * Position 1 : Symbol
			 * Position 2 : Last Price
			 * Position 3 : Change
			 * Position 4 : Bid
			 * Position 5 : Ask
			 * Position 6 : volume
			 * Position 7 : open interest
			 */
			
			if (colList == null || colList.size() < 3) {
				// System.out.println("No data in colList");
			} else {
				if (header == false) {
					
					lastPrice = StringUtils.strToFloat(colList.get(2));
					bidPrice = StringUtils.strToFloat(colList.get(4));
					askPrice = StringUtils.strToFloat(colList.get(5));
					
					premium = lastPrice;
					if (lastPrice > bidPrice && bidPrice > 0 && askPrice > 0) {
						premium = (bidPrice + askPrice)/2;
					}
					
					opt = new PutOption(stockPrice,
							colList.get(1),
							cal.getTime(),
				            StringUtils.strToFloat(colList.get(0)),
				            premium,
				       	    StringUtils.strToLong(colList.get(6)), 
							StringUtils.strToLong(colList.get(7)));  
					putOptionList.add(opt);							
				} else {
					header = false;
				}
			}		
			currPosition = pos2 + 5;		
			
		}			
		
		return;
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}

}
